/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.publisher.service.impl;

import com.alien.admin.modules.publisher.domain.PublishProject;
import com.alien.admin.utils.ValidationUtil;
import com.alien.admin.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.alien.admin.modules.publisher.repository.PublishProjectRepository;
import com.alien.admin.modules.publisher.service.PublishProjectService;
import com.alien.admin.modules.publisher.service.dto.PublishProjectDto;
import com.alien.admin.modules.publisher.service.dto.PublishProjectQueryCriteria;
import com.alien.admin.modules.publisher.service.mapstruct.PublishProjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.alien.admin.utils.PageUtil;
import com.alien.admin.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhouseyi
* @date 2021-11-03
**/
@Service
@RequiredArgsConstructor
public class PublishProjectServiceImpl implements PublishProjectService {

    private final PublishProjectRepository publishProjectRepository;
    private final PublishProjectMapper publishProjectMapper;

    @Override
    public Map<String,Object> queryAll(PublishProjectQueryCriteria criteria, Pageable pageable){
        Page<PublishProject> page = publishProjectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(publishProjectMapper::toDto));
    }

    @Override
    public List<PublishProjectDto> queryAll(PublishProjectQueryCriteria criteria){
        return publishProjectMapper.toDto(publishProjectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PublishProjectDto findById(Long id) {
        PublishProject publishProject = publishProjectRepository.findById(id).orElseGet(PublishProject::new);
        ValidationUtil.isNull(publishProject.getId(),"PublishProject","id",id);
        return publishProjectMapper.toDto(publishProject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PublishProjectDto create(PublishProject resources) {
        return publishProjectMapper.toDto(publishProjectRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PublishProject resources) {
        PublishProject publishProject = publishProjectRepository.findById(resources.getId()).orElseGet(PublishProject::new);
        ValidationUtil.isNull( publishProject.getId(),"PublishProject","id",resources.getId());
        publishProject.copy(resources);
        publishProjectRepository.save(publishProject);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            publishProjectRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PublishProjectDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PublishProjectDto publishProject : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("流程id", publishProject.getFlowId());
            map.put("项目id", publishProject.getProjectId());
            map.put("发布分支", publishProject.getBranch());
            map.put("发布版本号", publishProject.getVersion());
            map.put("上线方式", publishProject.getGray());
            map.put("实例数量", publishProject.getReplicas());
            map.put("灰度数量", publishProject.getGrayReplicas());
            map.put("资源规格", publishProject.getResourceId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
